package com.atm.thirdplt.common.utils.xml;

import com.atm.thirdplt.common.core.domain.his.HisPatsInHospital;
import com.atm.thirdplt.common.core.domain.his.ZLXmlBean;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.MapperWrapper;
import com.thoughtworks.xstream.security.AnyTypePermission;

import java.util.TimeZone;

/**
 * @ClassName XStreamUtil
 * @Description xml与对象转换工具类
 * @Author wenyf
 * @Version 1.0
 **/
public class XStreamUtil {
    private static String XML_TAG = "<?xml version='1.0' encoding='UTF-8'?>";

    /**
     * Description: 私有化构造
     */
    private XStreamUtil() {
        super();
    }

    /**
     * @return
     * @Description 为每次调用生成一个XStream
     * @Title getInstance
     */
    private static XStream getInstance() {
        XStream xStream = new XStream(new DomDriver("UTF-8")) {
            /**
             * 忽略xml中多余字段
             */
            @Override
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new MapperWrapper(next) {
                    @SuppressWarnings("rawtypes")
                    @Override
                    public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                        if (definedIn == Object.class) {
                            return false;
                        }
                        return super.shouldSerializeMember(definedIn, fieldName);
                    }
                };
            }
        };

        xStream.registerConverter(new CustomDateConverter("yyyy-MM-dd HH:mm:ss", new String[]{"yyyy-MM-dd", "HH:mm:ss"
                , "yyyy/MM/dd HH:mm:ss", "yyyy/M/dd", "yyyy/MM/d", "yyyy/M/d"}, TimeZone.getTimeZone("Asia/Shanghai"
        )));

        // 设置默认的安全校验
        XStream.setupDefaultSecurity(xStream);
        // 使用本地的类加载器
        xStream.setClassLoader(XStreamUtil.class.getClassLoader());
        // 允许所有的类进行转换
        xStream.addPermission(AnyTypePermission.ANY);
        return xStream;
    }

    /**
     * @param xml
     * @param clazz1
     * @return
     * @Description 将xml字符串转化为java对象
     * @Title xmlToBean
     */
    public static <T> T xmlToBean(String xml, Class<T> clazz1, Class clazz2) {
        XStream xStream = getInstance();
        xStream.processAnnotations(new Class[]{clazz1, clazz2});
        Object object = xStream.fromXML(xml);
        T cast = clazz1.cast(object);
        return cast;
    }

    /**
     * @param object
     * @return
     * @Description 将java对象转化为xml字符串
     * @Title beanToXml
     */
    public static String beanToXml(Object object) {
        XStream xStream = getInstance();
        xStream.processAnnotations(object.getClass());
        // 剔除所有tab、制表符、换行符
        String xml = xStream.toXML(object).replaceAll("\\s+", " ");
        return xml;
    }

    /**
     * @param object
     * @return
     * @Description 将java对象转化为xml字符串（包含xml头部信息）
     * @Title beanToXml
     */
    public static String beanToXmlWithTag(Object object) {
        String xml = XML_TAG + beanToXml(object);
        return xml;
    }


    /**
     * 测试main方法
     */
    public static void main(String[] args) {
        String xml = "<Rows><Row>\n" +
                "        <PatientId>470962</PatientId>\n" +
                "        <VisitId>1</VisitId>\n" +
                "        <WardCode>103</WardCode>\n" +
                "        <WardCode>104</WardCode>\n" +
                "        <DeptCode>103</DeptCode>\n" +
                "        <BedNo>21</BedNo>\n" +
                "        <AdmissionDateTime>2016-10-18 09:43:02</AdmissionDateTime>\n" +
                "        <AdmWardDateTime>2016-10-18 09:43:02</AdmWardDateTime>\n" +
                "        <DoctorInCharge>陈庚</DoctorInCharge>\n" +
                "        <Diagnosis>神经衰弱症</Diagnosis>\n" +
                "      </Row>\n<Row>\\n\" +\n" +
                "                \"        <PatientId>470962</PatientId>\\n\" +\n" +
                "                \"        <VisitId>2</VisitId>\\n\" +\n" +
                "                \"        <WardCode>102</WardCode>\\n\" +\n" +
                "                \"        <BedNo>22</BedNo>\\n\" +\n" +
                "                \"        <AdmissionDateTime>2016-10-18 09:43:02</AdmissionDateTime>\\n\" +\n" +
                "                \"        <AdmWardDateTime>2016-10-18 09:43:02</AdmWardDateTime>\\n\" +\n" +
                "                \"        <DoctorInCharge>陈庚2</DoctorInCharge>\\n\" +\n" +
                "                \"        <Diagnosis>神经衰弱症2</Diagnosis>\\n\" +\n" +
                "                \"      </Row>\\n</Rows>";

        //xml = xml.replace("<Row>", "<Row class='com.atm.thirdplt.common.core.domain.his.HisPatsInHospital'>");
        ZLXmlBean o = XStreamUtil.xmlToBean(xml, ZLXmlBean.class, HisPatsInHospital.class);
        System.out.println(11);
    }
}
